package com.liyunc.demo.bundle.file.manager.dao.repository;

import com.liyunc.demo.bundle.file.manager.model.entity.AssetFile;
import com.liyunc.demo.bundle.file.manager.model.entity.inline.AssetFileNeedPackInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

@Repository
public interface AssetFileEntityRepository extends JpaRepository<AssetFile, Long> {

    @Modifying(clearAutomatically = true, flushAutomatically = true)
    @Query(value = "UPDATE AssetFile asset SET"
        + " asset.filepath = ?1 , asset.version = ?2 where asset.id = ?3 AND "
        + " asset.version = ?4")
    int saveAndFlushByVersion(String newPath, int newVersion, long id, int oldVersion);

    @Query("SELECT afe.id as id, afe.filepath as path, afe.updatedAt as updatedAt "
        + "FROM AssetFile AS afe WHERE afe.id IN (:ids) "
        + "ORDER BY afe.updatedAt DESC")
    List<AssetFileNeedPackInfo> findFiles(@Param("ids") Set<Long> ids);
}
